/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evolve.webbot.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import org.apache.log4j.Logger;

/**
 *
 * @author DarthAE
 */
public class TxtFile {
    
    private String path;
    
    static Logger log = Logger.getLogger( TxtFile.class );
    
    public TxtFile(){}
    
    public TxtFile(String path){
        this.path = path;
    }
    
    public void writeFile(String path,String content)
    {
       FileWriter fichero = null;
       PrintWriter pw = null;
       
       try{                       
            fichero = new FileWriter(path);
            pw = new PrintWriter(fichero);
            pw.println(content);
          
           } catch (Exception e) {log.error("error al generar archivo de datos ",e);}
             finally {
                       try {
                               if (null != fichero)
                                   fichero.close();
                            } catch (Exception e2) {log.error("error al cerrar archivo de datos ",e2);}
                     }        
    }
    
    public ArrayList<String> getLinesFromFile(String filePath)
    {
      ArrayList<String> lines = new ArrayList<String>();
            
      try {
             BufferedReader br = new BufferedReader(new FileReader(filePath));
             String linea = br.readLine();
             
             while(linea!=null)
             {
                 lines.add(linea);
                 linea = br.readLine();
             }             
             
           }catch(IOException ioe){log.error("no se pudo recuperar archivo origen",ioe);}
      
      if(lines!=null)
      {
          log.info("Líneas recuperadas del archivo: "+lines.size());
      }else{
          log.info("No se ha recuperado información");
      }      
      
      return lines;
    }
    
    public void writeLiverpoolFile(String path,ArrayList<String> lines)
    {
       PrintWriter pw   = null;
       
       try{     
             File file = new File(path);
             FileWriter fw    = new FileWriter(file.getAbsoluteFile()); 
             BufferedWriter bw = new BufferedWriter(fw);
                       
             pw      = new PrintWriter(file);
                        
             for(int i=0; i<lines.size();i++)
             {
                 if(!lines.get(i).contains("Resultado"))
                 {
                    String line = lines.get(i);
                    line = line.replace("\"","");
                    bw.write(line+"\n");
                 }                            
             }                         
                        
             bw.close();
                        
          } catch (Exception e) {e.printStackTrace();}         
    }
}
